package com.tianyang.yukuaifuzs.business.trade.view;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.alibaba.fastjson.JSONObject;
import com.tianyang.yukuaifuzs.R;
import com.tianyang.yukuaifuzs.common.utils.DateCheckPopListener;
import com.tianyang.yukuaifuzs.module.dialog.AchievementDialog;
import com.tianyang.yukuaifuzs.module.dialog.interfaces.IAchieveListener;
import com.tianyang.yukuaifuzs.module.dialog.interfaces.ICallbackListener;
import com.tianyang.yukuaifuzs.module.roundview.RoundTextView;
import com.tianyang.yukuaifuzs.module.wheel.interfaces.IWheelDataChangeCallback;
import com.tianyang.yukuaifuzs.utils.DateUtils;
import com.tianyang.yukuaifuzs.utils.VerifyUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Description：商户详情统计
 * Created by xyj on 2024/3/26
 */
public class SubMerchantHeaderView extends LinearLayout {

    @BindView(R.id.filterTv)
    TextView filterTv;
    @BindView(R.id.dayFilterRtv)
    RoundTextView dayFilterRtv;
    @BindView(R.id.monthFilterRtv)
    RoundTextView monthFilterRtv;

    @BindView(R.id.mchNameTv)
    TextView mchNameTv;
    @BindView(R.id.totalAmountTv)
    TextView totalAmountTv;
    @BindView(R.id.totalProfitTv)
    TextView totalProfitTv;

    private AchievementDialog vAchievementDialog;

    private DateCheckPopListener mDatePopListener;

    private boolean isMonthFilter;

    private String startTime, endTime;

    private ICallbackListener<String> callbackListener;

    public SubMerchantHeaderView(Context context, AttributeSet attrs) {
        super(context, attrs);
        View.inflate(getContext(), R.layout.layout_sub_merchant_header, this);
        ButterKnife.bind(this);
    }

    public void setCallbackListener(ICallbackListener<String> callbackListener) {
        this.callbackListener = callbackListener;
    }

    @OnClick({R.id.filterTv, R.id.dayFilterRtv, R.id.monthFilterRtv})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.filterTv:
                if (isMonthFilter) {
                    if (mDatePopListener == null) {
                        mDatePopListener = new DateCheckPopListener((Activity) getContext(), true);
                    }
                    mDatePopListener.handleRegionCheck(view, "请选择月份", false, new IWheelDataChangeCallback() {
                        @Override
                        public void wheelDataChangeCallback(String wheelData) {
                            setTimeData(wheelData, wheelData);
                        }
                    });
                } else {
                    if (vAchievementDialog == null) {
                        vAchievementDialog = new AchievementDialog(getContext());
                        vAchievementDialog.setListener(new IAchieveListener() {
                            @Override
                            public void callback(String startTime, String endTime) {
                                setTimeData(startTime, endTime);
                            }
                        });
                    }
                    vAchievementDialog.setTimeData(startTime, endTime);
                    vAchievementDialog.show();
                }
                break;
            case R.id.dayFilterRtv:
                if (isMonthFilter) {
                    isMonthFilter = false;
                    showDayOrMonthView();
                    setTimeData(DateUtils.getTimeAgo(30, "yyyy-MM-dd"), DateUtils.getCurrentTime("yyyy-MM-dd"));
                }
                break;
            case R.id.monthFilterRtv:
                if (!isMonthFilter) {
                    isMonthFilter = true;
                    showDayOrMonthView();
                    setTimeData(DateUtils.getCurrentMonth(), DateUtils.getCurrentMonth());
                }
                break;
        }
    }

    public void initData(String mchName, String startTime, String endTime, boolean isMonthFilter) {
        mchNameTv.setText(VerifyUtil.checkString(mchName));
        this.startTime = startTime;
        this.endTime = endTime;
        this.isMonthFilter = isMonthFilter;
        showDayOrMonthView();
        setTimeData(startTime, endTime);
    }

    public void showDataView(JSONObject jsonObject) {
        if (jsonObject != null) {
            if (jsonObject.containsKey("totalAmount")) {
                totalAmountTv.setText(VerifyUtil.decimalFormat(jsonObject.getString("totalAmount")));
            }
            if (jsonObject.containsKey("totalProfit")) {
                totalProfitTv.setText(VerifyUtil.decimalFormat(jsonObject.getString("totalProfit")));
            }
        }
    }

    private void showDayOrMonthView() {
        if (isMonthFilter) {
            dayFilterRtv.setTextColor(ContextCompat.getColor(getContext(), R.color.c_999999));
            dayFilterRtv.getDelegate().setBackgroundColor(ContextCompat.getColor(getContext(), R.color.transparent));
            monthFilterRtv.setTextColor(ContextCompat.getColor(getContext(), R.color.black));
            monthFilterRtv.getDelegate().setBackgroundColor(ContextCompat.getColor(getContext(), R.color.white));
        } else {
            monthFilterRtv.setTextColor(ContextCompat.getColor(getContext(), R.color.c_999999));
            monthFilterRtv.getDelegate().setBackgroundColor(ContextCompat.getColor(getContext(), R.color.transparent));
            dayFilterRtv.setTextColor(ContextCompat.getColor(getContext(), R.color.black));
            dayFilterRtv.getDelegate().setBackgroundColor(ContextCompat.getColor(getContext(), R.color.white));
        }
    }

    @SuppressLint("SetTextI18n")
    public void setTimeData(String startTimeStr, String endTimeStr) {
        startTime = startTimeStr;
        endTime = endTimeStr;
        if (isMonthFilter) {
            filterTv.setText(startTime.replaceAll("-", "/"));
        } else {
            filterTv.setText(startTime.replaceAll("-", "/") + "-" + endTime.replaceAll("-", "/"));
        }
        if (callbackListener != null) {
            callbackListener.callback(startTime+","+endTime);
        }
    }
}
